package com.genghis.ptas.comment.service.impl;

import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.comment.dao.CommentDao;
import com.genghis.ptas.comment.entity.Comment;
import com.genghis.ptas.comment.service.CommentService;
import com.genghis.ptas.security.entity.User;
import com.genghis.ptas.security.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * User: tiangc
 * Date: 14-4-22
 * Time: 下午8:16
 * To change this template use File | Settings | File Templates.
 */
@Transactional
@Service("commentService")
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private UserService userService;

    @Override
    public void addScormComment(Comment comment,int userId) {
        comment.setUserId(userId);
        commentDao.addScormComment(comment);
    }

    @Override
    public List<Comment> findCommentListByScormId(int scormId) {
        return commentDao.findCommentListByScormId(scormId);
    }

    @Override
    public Comment findCommentByScormIdAndUseId(int scormId, int userId) {
        return commentDao.findCommentByScormIdAndUseId(scormId, userId);
    }

    @Override
    public Comment findCommentById(int id) {
        Comment comment= commentDao.findCommentById(id);
        comment.setCommentUser(userService.findUserAllInfoById(comment.getUserId()).getLoginName());
        return comment;
    }

    @Override
    public boolean findIsCommentById(int scormId, int userId) {
        int isCom=commentDao.findIsCommentById(scormId, userId);
        if(isCom==0){
           return true;
        }
        return false;
    }

    @Override
    public Page<Comment> listScormComment(PageParameter pageParameter, Comment comment) {
         Page<Comment> commentPage=commentDao.listScormComment(pageParameter,comment);
        for(Comment comment1:commentPage.getResultList()){
            User user=userService.findUserAllInfoById(comment1.getUserId());
            comment1.setCommentUser(user.getLoginName());
        }
        return commentPage;
    }

    @Override
    public void deleteCommentById(String[] ids) {
        for(String id:ids){
            commentDao.deleteCommentByid(Integer.parseInt(id));
        }
    }

    @Override
    public void editComment(Comment comment) {
        commentDao.editComment(comment);
    }
}
